package com.raft.core.log;

import java.util.Arrays;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

import com.raft.core.log.entry.Entry;
import com.raft.core.log.entry.NoOpEntry;

public class MemoryLogTest2 {

	// 没有冲突的
	// (index, term)
    // follower: (1, 1), (2, 1)
    // leader  :         (2, 1), (3, 2)
	@Test
    public void testAppendEntriesFromLeaderSkip() {
        MemoryLog log = new MemoryLog();
        log.appendEntry(1); // 1
        log.appendEntry(1); // 2
        List<Entry> leaderEntries = Arrays.asList(
                new NoOpEntry(2, 1),
                new NoOpEntry(3, 2)
        );
        Assert.assertTrue(log.appendEntriesFromLeader(1, 1, leaderEntries));
    }

	// 有冲突的
	// follower: (1, 1), (2, 1)
    // leader  :         (2, 2), (3, 2)
	// result  : (1, 1), (2, 2), (3, 2)
    @Test
    public void testAppendEntriesFromLeaderConflict1() {
        MemoryLog log = new MemoryLog();
        log.appendEntry(1); // 1
        log.appendEntry(1); // 2
        List<Entry> leaderEntries = Arrays.asList(
                new NoOpEntry(2, 2),
                new NoOpEntry(3, 2)
        );
        Assert.assertTrue(log.appendEntriesFromLeader(1, 1, leaderEntries));
    }

    // follower: (1, 1), (2, 1), (3, 1)
    // leader  :         (2, 1), (3, 2)
    // result  : (1, 1), (2, 1), (3, 2)
    @Test
    public void testAppendEntriesFromLeaderConflict2() {
        MemoryLog log = new MemoryLog();
        log.appendEntry(1); // 1
        log.appendEntry(1); // 2
        log.appendEntry(1); // 3
        List<Entry> leaderEntries = Arrays.asList(
                new NoOpEntry(2, 1),
                new NoOpEntry(3, 2)
        );
        Assert.assertTrue(log.appendEntriesFromLeader(1, 1, leaderEntries));
    }

    // follower: (1, 1), (2, 1), (3, 1)
    // leader  :         (2, 1), (3, 2), (4, 2), (5, 3)
    // result  : (1, 1), (2, 1), (3, 2), (4, 2), (5, 3)
    @Test
    public void testAppendEntriesFromLeaderConflict3() {
    	MemoryLog log = new MemoryLog();
    	log.appendEntry(1); // 1
    	log.appendEntry(1); // 2
    	log.appendEntry(1); // 3
    	List<Entry> leaderEntries = Arrays.asList(
    			new NoOpEntry(2, 1),
    			new NoOpEntry(3, 2),
    			new NoOpEntry(4, 2),
    			new NoOpEntry(5, 3)
    			);
    	Assert.assertTrue(log.appendEntriesFromLeader(1, 1, leaderEntries));
    }

}
